package com.sec.android.app.myfiles.module.download;

import android.content.Context;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.feature.PrivateModeMgr;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.module.abstraction.AbsMenuImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.module.shortcut.ShortcutUtils;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.util.FileUtils;

import java.util.ArrayList;

public class DownloadMenuImp extends AbsMenuImp {
    private boolean mFromDownloadHistory;
    private ArrayList<FileRecord> mContextualSelectedRecord;

    public DownloadMenuImp(Context context, StorageType supportStorage) {
        super(context, supportStorage);
    }

    @Override
    public String getActionBarTitle() {
        String ret;
        if (mNavigationInfo != null) {
            String path = mNavigationInfo.getCurRecord().getFullPath();
            mFromDownloadHistory = path.equals(AppConstants.StoragePath.DOWNLOAD_HISTORY)
                    || path.equals(AppConstants.StoragePath.DOWNLOAD_HISTORY_FROM_OTHERS);
        }
        if (AppFeatures.isTabletUIMode()) {
            ret = mContext.getString(R.string.app_name);
        } else {
            if (!mFromDownloadHistory) {
                ret = mContext.getString(R.string.downloads);
            } else {
                ret = mContext.getString(R.string.download_history);
            }
        }
        return ret;
    }

    @Override
    public void onPrepareNormalOptionsMenu(Menu menu) {
        super.onPrepareNormalOptionsMenu(menu);

        setMenuItemVisibility(menu, R.id.menu_clear_recent_history, false);
        setMenuItemVisibility(menu, R.id.menu_clear_history, false);
        setMenuItemVisibility(menu, R.id.menu_create_folder, false);
        setMenuItemVisibility(menu, R.id.menu_show_hidden_files, false);
        setMenuItemVisibility(menu, R.id.menu_hide_hidden_files, false);
        setMenuItemVisibility(menu, R.id.menu_manage_shortcut, ShortcutUtils.getShortcutItemsCount(mContext) > 0
                && AppFeatures.isTabletUIMode() && NavigationManager.isPossibleToActionMode());
    }

    @Override
    public void onPrepareEditOptionsMenu(Menu menu) {
        super.onPrepareEditOptionsMenu(menu);

        int selectedCount = mFragment.getSelectedFileCount();
        if (selectedCount > 0) {
            ArrayList<FileRecord> selectedItem = mFragment.getSelectedFile();
            if (isSharableRecords(selectedItem)) {
                setMenuItemVisibility(menu, R.id.menu_share, true);
            }
            if (!mFromDownloadHistory && isAllRecordsExist(selectedItem)) {
                if (AppFeatures.isTabletUIMode() && mFragment.getActionModeType() == AbsMyFilesFragment.ActionModeType.EDIT_FILE_FOLDER_OPERATION) {
                    showMoreMenu(menu, R.id.menu_move, false);
                }
                if (selectedCount == 1) {
                    FileRecord item = selectedItem.get(0);
                    if (item != null && ShortcutUtils.isSupportShortcut(mContext, item)) {
                        setMenuItemVisibility(menu, R.id.menu_add_shortcut, true);
                    }
                    setMenuItemVisibility(menu, R.id.menu_rename, true);
                }

                PrivateModeMgr privateModeMgr = PrivateModeMgr.getInstance(mContext);
                PrivatePath pathSts = PrivatePath.ONLY_NON_PRIVATE;
                if (privateModeMgr.isReady()) {
                    pathSts = getSelectedItemPrivatePathStatus(selectedItem);
                    setMenuItemVisibility(menu, R.id.menu_move_to_private, (pathSts != PrivatePath.ONLY_PRIVATE));
                    setMenuItemVisibility(menu, R.id.menu_remove_from_private, (pathSts == PrivatePath.ONLY_PRIVATE));
                }

                showKnoxMenu(menu, false, pathSts);
            } else {
                // If selected records include deleted record or current record is Download History via Internet,
                // only 'DELETE' and 'DETAILS' menu are available.
                setMenuItemVisibility(menu, R.id.menu_copy, false);
                setMenuItemVisibility(menu, R.id.menu_move, false);
                setMenuItemVisibility(menu, R.id.menu_rename, false);
            }
        }
    }

    private boolean isAllRecordsExist(ArrayList<FileRecord> selectedFile) {
        boolean bRet = selectedFile != null && !selectedFile.isEmpty();
        if (bRet) {
            for (FileRecord record : selectedFile) {
                if (!FileUtils.isSameAsRealFile(record)) {
                    bRet = false;
                    break;
                }
            }
        }
        return bRet;
    }

    @Override
    protected void createContextualMenuOnBackground(ContextMenu menu) {
        super.createContextualMenuOnBackground(menu);
        menu.removeItem(R.id.menu_create_folder);
        menu.removeItem(R.id.menu_paste);
    }

    @Override
    protected void createContextualMenuOnLeftPanel(ContextMenu menu, FileRecord record) {
    }

    @Override
    protected void _createContextualMenuOnItems(ContextMenu menu, FileRecord record, boolean isForOneItem) {
        mContextualSelectedRecord = getSelectedFileForContextMenu(mFragment, record);
        if (!mFromDownloadHistory && isAllRecordsExist(mContextualSelectedRecord)) {
            super._createContextualMenuOnItems(menu, record, isForOneItem);
            menu.removeItem(R.id.menu_paste);
            menu.removeItem(R.id.menu_zip);
            menu.removeItem(R.id.menu_unzip);
            menu.removeItem(R.id.menu_unzip_current_folder);
        } else {
            // If selected records include deleted record or current record is Download History via Internet,
            // only 'DELETE' and 'DETAILS' menu are available.
            menu.add(0, R.id.menu_delete, 0, mContext.getResources().getString(R.string.menu_delete));
            menu.add(0, R.id.menu_details, 0, mContext.getResources().getString(R.string.menu_details));
        }
    }

    @Override
    public boolean _onContextualItemSelected(MenuItem item, ArrayList<FileRecord> srcRecordList, FileRecord record, AbsMyFilesFragment fragment) {
        boolean bRet = false;
        if (fragment != null) {
            AbsMyFilesFragment.ActionModeType actionModeType = fragment.getActionModeType();
            if (actionModeType != AbsMyFilesFragment.ActionModeType.NORMAL) {
                record = mContextualSelectedRecord.get(0);
            }
            bRet = super._onContextualItemSelected(item, mContextualSelectedRecord, record, fragment);
            mContextualSelectedRecord = null;
        }
        return bRet;
    }

    @Override
    protected StorageType getTargetStorageType() {
        return StorageType.Downloads;
    }

    private ArrayList<FileRecord> getSelectedFileForContextMenu(AbsMyFilesFragment fragment, FileRecord record) {
        ArrayList<FileRecord> selectedFile;
        AbsMyFilesFragment.ActionModeType actionModeType = fragment.getActionModeType();
        if (actionModeType == AbsMyFilesFragment.ActionModeType.NORMAL) {
            selectedFile = new ArrayList<>();
            selectedFile.add(record);
        } else {
            selectedFile = fragment.getSelectedFile();
        }

        return selectedFile;
    }
}
